package com.promis.fastdfs;

import android.os.AsyncTask;

import com.promis.util.CollectionUtils;

import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * Created by jidanbo on 2018/3/16.
 */

public class FastFileStorageClient {

    public static void init(Properties properties) {
        try {
            ClientGlobal.initByProperties(properties);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static FastStore[] upload(final FastFile fastFile) {
        UploadTask myAsyncTask = new UploadTask();
        try {
            return myAsyncTask.execute(fastFile).get(1, TimeUnit.MINUTES);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static FastStore[] upload(final FastFile[] fastFiles) {
//        UploadTask myAsyncTask = new UploadTask(listener);
        UploadTask myAsyncTask = new UploadTask();
        try {
            return myAsyncTask.execute(fastFiles).get(1, TimeUnit.MINUTES);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static boolean delete(FastStore... fastStores) {
        DeleteTask myAsyncTask = new DeleteTask();
        try {
            return myAsyncTask.execute(fastStores).get(1, TimeUnit.MINUTES);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
        return false;
    }

    private static class UploadTask extends AsyncTask<FastFile, Integer, FastStore[]> {

//        private FastFileStorageListener listener;

//        public UploadTask(FastFileStorageListener listener) {
//            this.listener = listener;
//        }

        @Override
        protected FastStore[] doInBackground(FastFile... fastFiles) {
            if (!CollectionUtils.isNullOrEmpty(fastFiles)) {
                final FastStore[] fastStores = new FastStore[fastFiles.length];
                try {
                    StorageClient storageClient = StorageClientFactory.build();
                    for (int i = 0; i < fastFiles.length; i++) {
                        String[] fileIds = null;
                        if (fastFiles[i].getClass().equals(FastFileBytes.class)) {
                            FastFileBytes ff = (FastFileBytes) fastFiles[i];
                            fileIds = storageClient.upload_file(ff.getData(), ff.getExtName(), ff.getMetas());
                        } else if (fastFiles[i].getClass().equals(FastFilePath.class)) {
                            FastFilePath ff = (FastFilePath) fastFiles[i];
                            fileIds = storageClient.upload_file(ff.getFilePath(), ff.getExtName(), ff.getMetas());
                        }

                        if (!CollectionUtils.isNullOrEmpty(fileIds)) {
                            fastStores[i] = new FastStore(fileIds[0], fileIds[1]);
                        } else throw new RuntimeException("上传失败");
                    }
                    return fastStores;
                } catch (Exception ex) {
                    ex.printStackTrace();
                    delete(fastStores);
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(FastStore[] fastStores) {
            super.onPostExecute(fastStores);
            //当任务执行完成是调用,在UI线程
//            listener.onSuccess(fastStores);
        }
    }

    private static class DeleteTask extends AsyncTask<FastStore, Integer, Boolean> {

        @Override
        protected Boolean doInBackground(FastStore... fastStores) {
            if (!CollectionUtils.isNullOrEmpty(fastStores)) {
                try {
                    StorageClient storageClient = StorageClientFactory.build();
                    if (!CollectionUtils.isNullOrEmpty(fastStores)) {
                        for (FastStore fastStore : fastStores) {
                            storageClient.delete_file(fastStore.getGroup(), fastStore.getPath());
                        }
                    }
                    return true;
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            return false;
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            super.onPostExecute(aBoolean);
        }
    }
}
